<template>
  <div class="leave-wrap">
    <div class="leave-box">
      <span class="title">离职日期:</span>
      <datetime format="YYYY-MM-DD" v-model="month" class="date-time" :end-date="date" placeholder="请选择离职日期"></datetime>
      <img src="./in.png" class="pic1" onclick="return false"/>
    </div>
    <div class="leave-box">
      <span class="title">离职方式:</span>
      <popup-picker :data="wayList" v-model="way" class="way" placeholder="请选择离职方式"></popup-picker>
      <img src="./in.png" class="pic1" onclick="return false"/>
    </div>

    <div class="leave-reason">离职原因：</div>
    <div class="reason-box">
      <textarea class="content" placeholder="请输入离职原因" v-model="remark" @blur="inputBlur" maxlength="30"></textarea>
    </div>

    <div class="btn-box" v-show="hideBtn">
      <button type="button" class="btn" @click="confirm">确定</button>
    </div>
  </div>
</template>

<script>
  import {formatDate} from "../../../config/util"
  import {Datetime, PopupPicker} from 'vux'
  import {baseUrl} from "../../../config/env"

  export default {
    data() {
      return {
        id: "",
        userId: "",
        mechanismId: "",
        date: "",
        month: "",
        way: [],
        wayList: [["辞职", "自离"]],
        remark: "",
        type: "",
        docmHeight: document.documentElement.clientHeight,//默认屏幕高度
        showHeight: document.documentElement.clientHeight,//实时屏幕高度
        hideBtn: true//显示或者隐藏
      }
    },
    mounted() {
      this.id = this.$route.query.id;
      this.userId = this.$route.query.userId;
      this.type = this.$route.query.type;
      this.mechanismId = this.$route.query.mechanismId;
      let base64 = require('js-base64').Base64;
      this.userSign = base64.decode(sessionStorage.getItem("success"));
      this.date = formatDate(new Date(), "yyyy-MM-dd");
      window.onresize = () => {
        return (() => {
          this.showHeight = document.body.clientHeight;
        })()
      };
    },
    methods: {
      inputBlur() {
        window.scrollTo(0, 0);
      },
      confirm() {//确定
        if (this.month == "") {
          this.$vux.toast.text("请选择离职日期", "middle");
          return;
        }
        if (this.remark == "" || this.remark == undefined) {
          this.$vux.toast.text("请输入离职原因", "middle");
          return;
        }

        this.$vux.loading.show({
          text: '加载中',
        });

        let data = {
          "experienceId": this.id,
          "userId": this.userId,
          "workEndType": String(this.way) == "自离" ? 1 : 2,
          "workEndTime": this.month,
          "workEndDetails": this.remark
        };

        let time = (new Date()).getTime();
        let url = baseUrl + "/resident/insert_user_work_record?timestamp=" + time;
        let param = {"sgin": this.userSign, "timestamp": time};
        let signature = this.$signature(url, param);

        this.axios.post(baseUrl + "/resident/insert_user_work_record?sgin=" + this.userSign + "&type=" + this.type + "&auth=" + signature + "&timestamp=" + time, data).then((res) => {
          let resultObj = res.data;
          this.$vux.loading.hide();
          if (resultObj.code == 0) {
            if (resultObj.data == 0) {
              this.$vux.toast.text("离职失败，请重试", "middle");
            } else {
              this.$router.push({path: "/enterpriselist", query: {"mechanismId": this.mechanismId, "type": this.type}});
            }
          } else {
            this.$vux.toast.text(resultObj.msg, "middle");
          }
        }).catch(() => {
          this.$vux.loading.hide();
          this.$vux.toast.text("网络异常", "middle");
        });

      }
    },
    watch: {
      showHeight() {
        if (this.docmHeight > this.showHeight) {
          this.hideBtn = false;
        } else {
          this.hideBtn = true;
        }
      }
    },
    components: {
      Datetime,
      PopupPicker
    }
  }
</script>

<style lang="stylus" scoped>
  @import "../../../assets/stylus/mixin.styl"
  .leave-wrap
    wh(100%, 100%)
    background: rgba(255, 255, 255, 1)
    overflow: hidden
    .leave-box
      display: flex
      align-items: center
      wh(100%, 1.28rem)
      border-bottom: 0.027rem solid rgba(242, 242, 242, 1)
      .title
        margin-left: 0.32rem
        size-color(0.427rem, rgba(51, 51, 51, 1))
      .pic1
        margin-left: auto
        margin-right: 0.32rem
        wh(0.32rem, 0.32rem)
      .date-time
        flex: 1
        text-align: right
        padding-right: 0.267rem
        size-color(0.427rem, rgba(51, 51, 51, 1))
      .date-time >>> .weui-cell__ft .vux-cell-placeholder
        color: rgba(204, 204, 204, 1)
      .way
        flex: 1
        text-align: right
        padding-right: 0.267rem
        size-color(0.427rem, rgba(60, 175, 255, 1))
      .way >>> .vux-cell-primary .vux-popup-picker-select .vux-popup-picker-placeholder
        color: rgba(204, 204, 204, 1)
    .leave-reason
      margin-top: 0.427rem
      margin-bottom: 0.307rem
      margin-left: 0.32rem
      size-color(0.427rem, rgba(51, 51, 51, 1))
    .reason-box
      margin: 0 auto
      width: 93.6%
      height: 2.667rem
      margin-bottom: 0.427rem
      .content
        padding-top: 0.267rem
        padding-left: 0.267rem
        width: 97%
        height: 2.387rem
        font-size: 0.373rem
        color: rgba(51, 51, 51, 1)
        line-height: 0.48rem
        border: 0.027rem solid rgba(224, 224, 224, 1)
        border-radius(0.053rem)
        resize: none
    .btn-box
      position: absolute
      bottom: 0.64rem
      display: flex
      justify-content: center
      align-items: center
      width: 100%
      height: 1.28rem
      .btn
        width: 90.4%
        height: 1.28rem
        line-height: 1.28rem
        background: rgba(60, 175, 255, 1)
        font-size: 0.453rem
        color: rgba(255, 255, 255, 1)
        font-weight: 500
        border-radius: 0.16rem
</style>
